package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品相关接口

 * @Author: zyx
 * @CreateTime: 2024-10-10
 */

@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {
//    注入service
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
     @PostMapping
     @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
         dishService.saveWithFlavor(dishDTO);
         return Result.success();

    }

    /**
     * 菜品分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishQueryPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.queryPage(dishPageQueryDTO);
        return  Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据Id查到菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据Id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
       DishVO dishVO =  dishService.getByIdWithFlavor(id);
        return Result.success(dishVO );
    }

    /**
     * 更新菜品即修改菜品
     * @param dishDTO
     * @return
     */
//    修改肯定要返回修改后的数据
    //先接收到前端传回的数据
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
//        根据插入菜品可判断修改一条菜品同时可能修改n条口味
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

}
